package org.jeecg.modules.app.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.app.util
 * @Author: mi
 * @CreateTime: 2024-07-13 13:54
 * @Description:
 */
public class NumberArithmeticUtils {
    /**MI  微信回调被除法运算
     * 微信除100 运算
     * @param total_fee  被计算的金额
     * @return
     */
    public static String getdivision(String total_fee ){
        //除法100
        BigDecimal Amount=new BigDecimal(total_fee);
        BigDecimal bd2 = new BigDecimal(100);
        String money=Amount.divide(bd2, 2, BigDecimal.ROUND_HALF_UP).doubleValue()+ "";
        return money;
    }
    //比例换算
    public static BigDecimal proportionPrice(BigDecimal one ,BigDecimal two){
        String b=one.divide(two,2, BigDecimal.ROUND_HALF_UP).doubleValue()+ "";
        BigDecimal bd2 = new BigDecimal("100");
        return bd2.multiply(new BigDecimal(b));
    }





    /**mi
     * 除发运算 division
     * @param one 除数1
     * @param two  除数2
     * @return
     */
    public static String division(BigDecimal one ,BigDecimal two){
        return  one.divide(two,2, BigDecimal.ROUND_HALF_UP).doubleValue()+ "";
    }


    /**mi
     * 乘法运算
     * @param one 参数1
     * @param two 参数2
     * @return
     */
    public static BigDecimal multiplication(BigDecimal one ,BigDecimal two){
        BigDecimal returnAmount=one.multiply(two);
        String result = String.format("%.2f", returnAmount);
        return new BigDecimal(result);
//        returnAmount.setScale(2, RoundingMode.HALF_UP);
//       return returnAmount;
    }

    /**mi
     * 减法运算
     * @param one 金额1
     * @param two 被减金额
     * @return
     */
    public static BigDecimal subtraction(BigDecimal one ,BigDecimal two){
        BigDecimal returnAmount= one.subtract(two);

        String result = String.format("%.2f", returnAmount);
        return new BigDecimal(result);
//        returnAmount.setScale(2, RoundingMode.HALF_UP);
//        return returnAmount;
    }
    /**mi
     * 加法
     * @param one 金额1
     * @param two 金额2
     * @return
     */
    public static BigDecimal add(BigDecimal one ,BigDecimal two){
        BigDecimal returnAmount= one.add(two);
        returnAmount.setScale(2, RoundingMode.HALF_UP);
        return returnAmount;
    }


    /**mi
     * string 类型的金额 转成Int类型 取整数
     * @param money
     * @return
     */
    public static Integer integerAmount(String money){
        Integer a = Integer.parseInt(money.substring(0,money.indexOf(".")));
        return a;
    }





    /**mi
     * 生成单号 流水号
     * @return
     */
    public static String getCode(){
        DateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmssSSS");
        String digital= getRandomLength(3);
        return sdf.format(new Date())+digital;
    }
    /**mi
     * 纯数字的随机数随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomLength(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    /**mi   微信支付
     * 获取一定长度的随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 当前时间戳+8位随机数
     * @return
     */
    public static String getTimestamp() {
        return System.currentTimeMillis()+NumberArithmeticUtils.getRandomLength(8);
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     * scw 2024-04-22
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s){
        try {
            if(s.indexOf(".") > 0){
                s = s.replaceAll("0+?$", "");//去掉多余的0
                s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
            }
        }catch (Exception e){
            return s;
        }
        return s;
    }
}
